<?php
/**
 * 游戏-老虎机
 */
session_start();
$uid = @$_SESSION['id'] ? @$_SESSION['id'] :''; //当前用户
if(!$uid){
	exit('请登录！');
}
//从game表中取值，查看游戏扣除积分
$xf = C_base::M('admin.gamegl')->game_s(array('id'=>2));
//从game_conf中取值
$tager = C_base::M('admin.gamegl')->gconf_ss(array('gameid'=>2),'jiangpin,jilv,level');

if($tager){
	$award =array();
	foreach ($tager as $k=>$v){
		$award[] = restore_array($v);
		//restore_array发放在底部，作用是将数组中的元素转换成数字；
		//例如:$a = array('id'=>111,'name'=>222,'mo'=>333);$a = restore_array($a);
		//$a的值就变成了array('0'=>111,'1'=>222,'2'=>333);
	}
}


$r =rand(1,100);
$num = 0;
$award_id = 0;
foreach($award as $k=>$v){
    $tmp = $num;
    $num += $v[1]*100;
    if($r>$tmp && $r<=$num){
    	$aa = $v;
        $award_id = $k;
        break;
    }
}

$data = array('jiangpin'=>$aa[0],'num'=>$aa[2]);
echo json_encode($data);

$record = array('uid'=>$uid,'gamename'=>$xf['name'],'jifen'=>$xf['jifen'],'time'=>time());
if($data['jiangpin']){
	$record['jiangpin'] = $data['jiangpin'];
	$record['zt'] = 1;
	//向game_winning表中写入数据
	C_base::M('admin.gamegl')->winn_c(array('uid'=>$uid,'gameid'=>2,'phone'=>'111','jiangpin'=>$data['jiangpin'],'time'=>time()));
}
//向game_record表中写入数据
C_base::M('admin.gamegl')->record_c($record); ////游戏记录

exit();

/**
 * php数组
 * key
 * 恢复key
 */
function restore_array($arr){
	if (!is_array($arr)){ return $arr; }
	$c = 0; $new = array();
	while (list($key, $value) = each($arr)){
		if (is_array($value)){
			$new[$c] = restore_array($value);
		}
		else { $new[$c] = $value; }
		$c++;
	}
	return $new;
}
?>